package com.dyz.LeetCodeforSwordFingerOffer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class LevelOrder32 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        //特殊数值处理
        if(root==null){return res;}
        //思路BFS广度搜索 队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()){
            List<Integer> list = new ArrayList<>();
            int size = queue.size();
            for (int i=0; i<size;i++){
                TreeNode node = queue.poll();
                list.add(node.val);
                //添加节点
                if(node.left!=null){queue.add(node.left);}
                if(node.right!=null){queue.add(node.right);}
            }
            res.add(list);
        }
        return res;
    }
}
